package Fonctions;

import java.awt.Event;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import InterFaces.IEvent;
import InterFaces.IPlugin;


public class Plugin_SearchNextDay implements IPlugin{


    public Plugin_SearchNextDay() {
         
    }

    
    public Date searchDate() {
    	
    	ArrayList<IEvent> listEventsUser = new ArrayList<IEvent>();
   	 File f = new File("events.txt");

 
   	try{
           //Lecture du fichier
               InputStream ips=new FileInputStream(f); 
                       InputStreamReader ipsr=new InputStreamReader(ips);
                       BufferedReader br=new BufferedReader(ipsr);
                       String ligne;
                       while ((ligne=br.readLine())!=null){
                           String[] tabString = ligne.split(",");
                                  
                           IEvent ev = new Data.Event(tabString[0], new Date(Long.parseLong(tabString[2])), new Date(Long.parseLong(tabString[3])));
                           listEventsUser.add(ev);                           
                      }
                       br.close(); 
         
           }
           catch (IOException e) {
           e.printStackTrace();
        }
    	
  
   	//Search
   	List <Date> eventDates = this.getDateTable(listEventsUser);
   	Date last = this.searchLastDay(eventDates);
   	List<Date> calendar = this.listeDesDates(new Date(), last);
	
   	return this.comparerList(calendar, eventDates).get(0);
   	
}


    @Override
	public String getRole() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public String getClasse() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Object getPlugin(IPlugin p) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	public List<Date> getDateTable(List<IEvent> events) {
		List <Date> dates = new ArrayList();;
		for (int i=0; i<events.size();i++) {
			
			dates.add(events.get(i).getDateDebut());
			
		}
		
		return dates;
	}
	
	
	public Date searchLastDay(List<Date> dates) {
		
		Date last = dates.get(0);
		for(int i=0;i<dates.size();i++){
			
			if(dates.get(i).after(last))
				last = dates.get(i);
			
		}
		
		return last;
	}
	
	
	
	public static List<Date> listeDesDates (Date DateDebut, Date DateFin)
	{
       List <Date> listeDates = new ArrayList();
 
		GregorianCalendar caldeb = new GregorianCalendar();
		caldeb.setTime(DateDebut);
		GregorianCalendar calfin = new GregorianCalendar();
		calfin.setTime(DateFin);
 
 
		while (caldeb.before(calfin))
		{
		listeDates.add(caldeb.getTime());
		caldeb.add(GregorianCalendar.DATE,1);
 
 
 
		}	
		//supprimer le dernier caractre (;)
		return listeDates;
	}
	
	
	public List<Date> comparerList(List<Date> calendar, List<Date> events){
		List<Date> dates = new ArrayList(); 
		for(int i=0;i<calendar.size();i++){
				if(events.contains(calendar.get(i))){
					dates.add(calendar.get(0));
				}
				
			}
		return dates;
	}
	
	
}
